﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SkyBase
{
    class Enemy:Player
    {

        public bool rotated = false; //Prevents us from rotating twice per update cycle. Look in onCollision and updateLocation. 
        Random rando;
        int maxCooldown = 20;
        int currentCooldown = 0; 

        //public int countDown;

        public Enemy(string name, World theWorld, Random random, Texture2D sprite = null)
            : base(name, theWorld, sprite)
        {
            direction = direction.BOTTOM;
            
            double temp = random.NextDouble();
            while(temp<(0.5)){
                temp = random.NextDouble();
            }
            double temp2 = (double)theWorld.Floor;
            Health = (int)(temp*(temp2 * 15.0));
            maxHealth = Health;
            direction = (direction)Enum.GetValues(direction.GetType()).GetValue(0);
            rando = random;
            maxCooldown = 150 - (theWorld.Floor*2);
            if (maxCooldown < 1) maxCooldown = 1;
        }

        public override void updateLocation(float x = 0, float y = 0)
        {

            updateDirection();

            Location += velocity;

            rotated = false;
            
        }


        /// <summary>
        /// Sets the enemy's velocity according to its direction.
        /// </summary>
        public void updateDirection()
        {
            if (currentCooldown < maxCooldown) currentCooldown++;
            if (Vector2.Distance(world.GetObject("player").Location, Location) < 300)
            {
                facePlayer();
            }
                switch (direction)
            {
                case (direction.TOP):
                    velocity = new Vector2(0, -0.5f);
                    break;
                case (direction.BOTTOM):
                    velocity = new Vector2(0, 0.5f);
                    break;
                case (direction.LEFT):
                    velocity = new Vector2(-0.5f, 0);
                    break;
                case (direction.RIGHT):
                    velocity = new Vector2(0.5f, 0);
                    break;
                case (direction.TOPLEFT):
                    velocity = new Vector2(-0.5f, -0.5f);
                    break;
                case (direction.TOPRIGHT):
                    velocity = new Vector2(0.5f, -0.5f);
                    break;
                case (direction.BOTTOMLEFT):
                    velocity = new Vector2(-0.5f, 0.5f);
                    break;
                case (direction.BOTTOMRIGHT):
                    velocity = new Vector2(0.5f, 0.5f);
                    break;
            }
            
            
        }

        public override void onCollision(GameObject collidee)
        {
            //Debugger.debug("CHANGING DIRECTION");

            if (!rotated && Vector2.Distance(world.GetObject("player").Location, Location) > 300)
            {
                Array values = Enum.GetValues(direction.GetType());

                int temp = World.random.Next(0, values.Length - 1);
                direction = (direction)values.GetValue(temp);

                while (direction == (direction)values.GetValue(8))
                {
                    temp = World.random.Next(0, values.Length - 1);
                    direction = (direction)values.GetValue(temp);
                }

                updateDirection();
                rotated = true;
            }

            //this.updateLocation();
            //Debugger.debug(direction.ToString());
            //this.Location += velocity*2;
        }

        private void facePlayer()
        {
            //Debugger.enabled = true;
            double angleInDegrees = Math.Atan2(world.GetObject("player").Location.Y - Location.Y, world.GetObject("player").Location.X - Location.X) * 180 / Math.PI;
            if (angleInDegrees < 0)
                angleInDegrees = 360 - Math.Abs(angleInDegrees);
            Debugger.writeLine(angleInDegrees.ToString());
            double tempAngle = 337.5;

            /*if (currentCooldown == maxCooldown)
            {
                World.enemyFireBullet(this);
                currentCooldown = 0;
            }*/

            foreach (direction x in Enum.GetValues(direction.GetType()))
            {
                
                if (x != direction.NONE)
                {
                    if (tempAngle > 360)
                        tempAngle = tempAngle - 360;
                    Debugger.writeLine("Current angle is " + angleInDegrees);
                    Debugger.writeLine("Checking to see if we're in the " + x + " direction");
                    Debugger.writeLine("Is " + angleInDegrees + " > " + tempAngle + " && " + angleInDegrees + " < " + (tempAngle + 45));
                    if(tempAngle + 45 < 360)
                        if (angleInDegrees > tempAngle && angleInDegrees < tempAngle + 45)
                        {
                            Debugger.writeLine("Found direction! Breaking");
                            direction = x;
                            break;
                        }
                    if(tempAngle + 45 > 360)
                        if (angleInDegrees > tempAngle || angleInDegrees < (tempAngle + 45) - 360 )
                        {
                            Debugger.writeLine("Found direction! Breaking");
                            direction = x;
                            break;
                        }
                    tempAngle += 45;
                }

            }

            
            //Debugger.enabled = false;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            
            spriteBatch.Draw(Sprite, Bounds, null
                          , color, (float)GetRotation(),
                          new Vector2(Sprite.Width / 4, Sprite.Height / 4), SpriteEffects.None, priority);
            double temp = (double)Health / (double)maxHealth;
            if (temp >= 0.5)
            {
                spriteBatch.DrawString(world.GetEngine().arialBold, Health.ToString(), new Vector2(Location.X, Location.Y - 30), Color.Green);
            }
            else if (temp < 0.5 && temp >= 0.2)
            {
                spriteBatch.DrawString(world.GetEngine().arialBold, Health.ToString(), new Vector2(Location.X, Location.Y - 30), Color.Goldenrod);
            }
            else spriteBatch.DrawString(world.GetEngine().arialBold, Health.ToString(), new Vector2(Location.X, Location.Y - 30), Color.Red);
        }
        public bool canFire()
        {
            if (currentCooldown < maxCooldown) return false;
            return true;
        }
        public void fired()
        {
            currentCooldown = 0;
        }
    }
}
